package com.jinloes.webfit;


import com.jinloes.webfit.persistence.User;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("userDao")
public class UserDaoHibernate extends HibernateDaoSupport implements UserDao {

    public User getUserByName(String name) {
        List<User> userList = getHibernateTemplate().find("from User u where u.username=?", name);
        return  !userList.isEmpty() ? userList.get(0) : null;
    }

    public void saveUser(User user) {
        getHibernateTemplate().save(user);
    }

    public void updateUser(User user) {
        getHibernateTemplate().update(user);
    }

    @Autowired
    public void setupSessionFactory(SessionFactory sessionFactory) {
        this.setSessionFactory(sessionFactory);
    }
    /*public void setSessionFactory() {
        this.setSessionFactory(HibernateHSQLUtils.getSessionFactory());
    }*/
}
